import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import {formatMessage} from './intl';
import stringify from 'Shared/utils/stringify';
const baseUrl = '/afterSales/api/v1';

const resourceName = formatMessage({
    id: 'api.resourceName',
    defaultMessage: '配件索赔分类标准'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询{resourceName}'
}, {resourceName});

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询{resourceName}详情'
}, {resourceName});

const addDesc = formatMessage({
    id: 'api.message.addDesc',
    defaultMessage: '新增{resourceName}'
}, {resourceName});

const updateDesc = formatMessage({
    id: 'api.message.updateDesc',
    defaultMessage: '编辑{resourceName}'
}, {resourceName});

const exportDesc = formatMessage({
    id: 'api.message.exportDesc',
    defaultMessage: '导出{resourceName}'
}, {resourceName});

const abandonDesc = formatMessage({
    id: 'api.message.abandonDesc',
    defaultMessage: '作废{resourceName}'
}, {resourceName});

// 查询
export const getPartWarrantyCategories = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partWarrantyCategories?${queryString}`, null)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询详情
export const getPartWarrantyCategoryDetail = id => request(`${baseUrl}/partWarrantyCategories/${id}`, null)
    .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

// 新增
export const addPartWarrantyCategory = data => request(`${baseUrl}/partWarrantyCategories`, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, addDesc));

// 编辑
export const updatePartWarrantyCategory = (id, data) => request(`${baseUrl}/partWarrantyCategories/${id}`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, updateDesc));

// 导出
export const exportPartWarrantyCategory = queryCondition => {
    const queryString = stringify(queryCondition);
    return request(`${baseUrl}/partWarrantyCategories/export?${queryString}`)
        .then(handleFetchResultNotification(REQUEST_TYPE.submit, exportDesc))
        .then(res => res.data);
};

// 作废
export const abandonPartWarrantyCategory = (id, data) => request(`${baseUrl}/partWarrantyCategories/${id}/abandon`, {
    method: 'PUT',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDesc));
